﻿Imports VendasBLL
Public Class ConsultaOrdemServico
    Dim ObjBLL As New OrdemServicoBLL
    Public ObjO As New OrdemServicoBLL
    Public Tipo As String
    Private Sub ConsultaOrdemServico_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CarregaGrid()
        CBFiltro.SelectedIndex = 1
        CBFiltro.Focus()
    End Sub
#Region "Métodos"
#Region "Carrega grid"
    Sub CarregaGrid()
        DGVDado.DataSource = ObjBLL.buscar("", 5)
        DGVDado.Refresh()
    End Sub
#End Region
#End Region
#Region "Eventos"
#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region

#Region "Localizar no textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Cliente"
                DGVDado.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Placa"
                DGVDado.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Data de inicio"
                BBuscar_Click(sender, e)
            Case "Status"
                DGVDado.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case Else
                DGVDado.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDado.DataSource = ObjBLL.buscar(FormatDateTime(Date.Now, DateFormat.ShortDate).ToString(), 7)
        End If
        DGVDado.Refresh()
    End Sub

#End Region

#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress, TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region

#Region "ComboboxFiltro SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 4 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(sender, e)
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
            CarregaGrid()
        End If
        If CBFiltro.SelectedIndex = 3 Then
            TXTLocalizar.Visible = False
            DTPInicial.Visible = True
            DTPFinal.Visible = True
            LBInformacao.Visible = True
            BBuscar.Visible = True
        Else
            BBuscar.Visible = False
            DTPInicial.Visible = False
            DTPFinal.Visible = False
            LBInformacao.Visible = False
        End If
        TXTLocalizar.Text = ""

    End Sub
#End Region

#Region "ComboboxStatus Selectedindex"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDado.DataSource = ObjBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDado_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDado.RowPrePaint
        If DGVDado.Rows(e.RowIndex).Cells(10).Value <> 5 Then
            DGVDado.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen
        End If
    End Sub
#End Region

#Region "Double click do datagrid"
    Private Sub DGVDado_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDado.CellDoubleClick
        Try

            If Tipo = "ret" Then
                Dim indice As Integer = -1
                'Percorre O DataGridView Verificando a Linha selecionada
                For Each linha As DataGridViewRow In DGVDado.SelectedRows
                    If (Not linha.Cells("ord_codigo") Is Nothing) Then
                        'Guarda A Posição Selecionada
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    If (DGVDado.Rows(indice).Cells("sta_codigo").Value <> 9) Then
                        If (DGVDado.Rows(indice).Cells("sta_codigo").Value = 5) Then
                            If MessageBox.Show("Deseja tornar a ordem de serviço nº: " & DGVDado.Rows(indice).Cells("ord_codigo").Value & " em uma venda?", "", MessageBoxButtons.YesNo) = vbYes Then
                                ObjO = New OrdemServicoBLL(Val(DGVDado.Rows(indice).Cells("ord_codigo").Value.ToString()))
                                ObjBLL = New OrdemServicoBLL(Val(DGVDado.Rows(indice).Cells("ord_codigo").Value.ToString()))
                                ObjBLL.CodStatus = 9
                                ObjBLL.IAEOrdem("A")
                                ObjBLL.Limpar()
                                Me.Close()
                            End If
                        Else : Throw New Exception("Ordem de serviço não foi concluída.")
                        End If
                    Else : Throw New Exception("Ordem de serviço já se transformou em uma venda.")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
#End Region
#End Region
#Region "Botões"
#Region "Buscar por datas"
    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        Try
            DGVDado.DataSource = ObjBLL.locdata(DTPInicial.Text, DTPFinal.Text)
            DGVDado.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region

#Region "Finalizar"
    Private Sub TSBFinalizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBFinalizar.Click
        Try
            Dim cod As Integer = 0
            Dim indice As Integer = -1
            For Each linha As DataGridViewRow In DGVDado.SelectedRows
                If (Not linha.Cells("ord_codigo") Is Nothing) Then
                    ObjBLL = New OrdemServicoBLL(Val(DGVDado.Rows(linha.Index).Cells("ord_codigo").Value.ToString()))

                End If
            Next
            If (ObjBLL.OrdCodigo > 0) Then
                TSBFinalizar.BackColor = Color.Gainsboro
                If (ObjBLL.CodStatus <> 5) Then
                    If (ObjBLL.Total > 0) Then
                        If MessageBox.Show("Confirma finalizção da ordem de serviço nº: " & ObjBLL.OrdCodigo & " ?", "", MessageBoxButtons.YesNo) = vbYes Then
                            ObjBLL.CodStatus = 5
                            If ObjBLL.DataFinal Is Nothing Then
                                ObjBLL.DataFinal = Date.Now
                            End If
                            ObjBLL.IAEOrdem("A")
                            CarregaGrid()
                        End If
                    Else : Throw New Exception("Não pode ser finalizada ordem de serviço sem produtos/serviços")
                    End If
                Else : Throw New Exception("Ordem de serviço nº " & ObjBLL.OrdCodigo & " já finalizada")
                End If
            Else : Throw New Exception("Selecione o ordem de serviço a ser finalizada")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBFinalizar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Novo"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        Try
            OrdemServicoFRM.Op = "I"
            TSBNovo.BackColor = Color.Gainsboro
            OrdemServicoFRM.ShowDialog()
            CarregaGrid()
        Catch ex As Exception
        Finally
            TSBNovo.BackColor = Color.Transparent
        End Try

    End Sub
#End Region

#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAbrir.Click

        Dim indice As Integer = -1
        Try
            For Each linha As DataGridViewRow In DGVDado.SelectedRows
                If (Not linha.Cells("ord_codigo") Is Nothing) Then
                    ObjBLL = New OrdemServicoBLL(Val(DGVDado.Rows(linha.Index).Cells("ord_codigo").Value.ToString()))
                End If
            Next
            If (ObjBLL.OrdCodigo > 0) Then
                If (ObjBLL.CodStatus <> 5) Then
                    OrdemServicoFRM.Codigo = ObjBLL.OrdCodigo
                    OrdemServicoFRM.Op = "A"
                    TSBAbrir.BackColor = Color.Gainsboro
                    OrdemServicoFRM.ShowDialog()
                    CarregaGrid()
                Else : Throw New Exception("Ordem de serviço nº " & ObjBLL.OrdCodigo & " já finalizada")
                End If

            Else : Throw New Exception("Selecione ordem de serviço para abrir")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Finally
            TSBAbrir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        ObjO = Nothing
        Me.Close()
    End Sub
#End Region
#End Region

End Class